import 'package:equatable/equatable.dart';

/// 单聊消息模型
/// 表示单聊中的文本消息
class SingleChatMessageModel extends Equatable {
  /// 消息ID
  final String id;

  /// 聊天ID
  final String chatId;

  /// 发送者ID
  final String senderId;

  /// 消息内容
  final String content;

  /// 发送时间
  final DateTime sendTime;

  /// 是否已读
  final bool isRead;

  /// 已读时间
  final DateTime? readTime;

  /// 构造函数
  const SingleChatMessageModel({
    required this.id,
    required this.chatId,
    required this.senderId,
    required this.content,
    required this.sendTime,
    this.isRead = false,
    this.readTime,
  });

  /// 从JSON创建实例
  factory SingleChatMessageModel.fromJson(Map<String, dynamic> json) {
    return SingleChatMessageModel(
      id: json['id'] as String,
      chatId: json['chatId'] as String,
      senderId: json['senderId'] as String,
      content: json['content'] as String,
      sendTime: DateTime.parse(json['sendTime'] as String),
      isRead: json['isRead'] as bool? ?? false,
      readTime:
          json['readTime'] != null
              ? DateTime.parse(json['readTime'] as String)
              : null,
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'chatId': chatId,
      'senderId': senderId,
      'content': content,
      'sendTime': sendTime.toIso8601String(),
      'isRead': isRead,
      'readTime': readTime?.toIso8601String(),
    };
  }

  /// 复制并修改属性
  SingleChatMessageModel copyWith({
    String? id,
    String? chatId,
    String? senderId,
    String? content,
    DateTime? sendTime,
    bool? isRead,
    DateTime? readTime,
  }) {
    return SingleChatMessageModel(
      id: id ?? this.id,
      chatId: chatId ?? this.chatId,
      senderId: senderId ?? this.senderId,
      content: content ?? this.content,
      sendTime: sendTime ?? this.sendTime,
      isRead: isRead ?? this.isRead,
      readTime: readTime ?? this.readTime,
    );
  }

  /// 标记为已读
  SingleChatMessageModel markAsRead() {
    return copyWith(isRead: true, readTime: DateTime.now());
  }

  /// 验证模型数据
  bool get isValid {
    return id.isNotEmpty &&
        chatId.isNotEmpty &&
        senderId.isNotEmpty &&
        content.isNotEmpty;
  }

  @override
  String toString() {
    return 'SingleChatMessageModel(id: $id, chatId: $chatId, senderId: $senderId, content: $content, sendTime: $sendTime, isRead: $isRead, readTime: $readTime)';
  }

  @override
  List<Object?> get props => [
    id,
    chatId,
    senderId,
    content,
    sendTime,
    isRead,
    readTime,
  ];
}
